package com.stu.manager.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.course.entity.SysCourse;
import com.stu.manager.course.entity.dto.CourseDto;
import com.stu.manager.course.service.SysCourseServiceImpl;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.personnel.entity.SysStudent;
import com.stu.manager.personnel.entity.dto.StudentDto;
import com.stu.manager.personnel.service.SysStudentServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/api/course")
@CrossOrigin
public class SysCourseController {

    @Autowired
    private SysCourseServiceImpl sysCourseService;
    @Autowired
    private LoginHandler loginHandler;

    @PostMapping("pageCourse/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示课程")
    public Result pageCourse(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody(required = false) CourseDto courseDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }

        Page<SysCourse> pageCourse = new Page<>(current, limit);
        QueryWrapper<SysCourse> wrapper = new QueryWrapper<>();
        String courseName = courseDto.getCourseName();
        String departmentName = courseDto.getDepartmentName();
        String professionalName = courseDto.getProfessionalName();

        if (!StringUtils.isEmpty(courseName)) {
            wrapper.like("course_name", courseName);
        }
        if (!StringUtils.isEmpty(departmentName)) {
            wrapper.like("department_name",departmentName);
        }
        if (!StringUtils.isEmpty(professionalName)) {
            wrapper.like("professional_name",professionalName);
        }

        IPage<SysCourse> sysCourseIPage = sysCourseService.pageCourse(pageCourse, wrapper);
        List<SysCourse> records = sysCourseIPage.getRecords();
        long total = sysCourseIPage.getTotal();

        return Result.ok().data("total", total).data("records", records);
    }

    @PostMapping("addCourse")
    @ApiOperation("添加课程")
    public Result addCourse(@RequestBody CourseDto courseDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = sysCourseService.addCourse(courseDto);
        return result;
    }

    @GetMapping("getCourseById/{id}")
    @ApiOperation("查询课程信息，修改回显")
    public Result getCourseById(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysCourse courseById = sysCourseService.getCourseById(id);
        return Result.ok().data("item", courseById);
    }

    @PostMapping("updateCourse")
    @ApiOperation("修改课程信息")
    public Result updateCourse(@RequestBody SysCourse sysCourse,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = sysCourseService.updateCourse(sysCourse);
        return result;
    }

    @DeleteMapping("delCourse/{id}")
    @ApiOperation("删除课程信息")
    public Result delCourse(@PathVariable String id,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        boolean remove = sysCourseService.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

    @PostMapping("updateCourseManage")
    @ApiOperation("修改课程教师时间信息")
    public Result updateCourseManage(@RequestBody SysCourse sysCourse,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = sysCourseService.updateCourseManage(sysCourse);
        return result;
    }
    
    @ApiOperation("根据老师查询本周课表")
    @GetMapping("findCoursePlan")
    public Result findCoursePlan(HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        String jobNum = request.getHeader("X-Token");
        List<SysCourse> coursePlan = sysCourseService.findCoursePlan(jobNum);

        return Result.ok().data("coursePlan",coursePlan);

    }

}

